
package com.spsy.pay.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.spsy.pay.domain.PayData;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCBj+KqN7dNPs2marqQ8OZ0ZTdNCdT+GBs8hp1gHcpe7h5AMosCsr3F78iQafr62i+a8RikeQbnFj8o07KBKWH0aDSI2S7plRn3RqVax6j5YvE1VAPu0QYxz+yj2F+61aoGCZpLz3ZGNVEj1amLSV788MHqMIzO50zLJhAGycVmnEZbuEeb6mSLRgcPTHDRS8kUi+ZXeVcISp6WXQbqlAIVG9dn8R+oCZMQgzDTY9Fm0mEENMbVStZ44O24P70a/0SULkwpwAZCmvi+vtcNs3IwFU5Z5+P4j2QmcyT1Dg2N6H3IeDQHVpbWLdvyi4aQKiYwqx8VTz/pwpTk2KIcdxvnAgMBAAECggEAGBEwW8pJxJGcBgPhcDcvuANtSGRBKooz1QR0i3w9MXEuMWKyK+gkFHa8XQcy1hJdsXyDxVmuDJM3XFAm4pNbXNlWmO0vJ7jIwARhhwF7xAe7ItreeChf7DThknYJIl26XbC+9sItCFGzJuLzwan5eScH/obSQUT6IOqLI5erkanF8cdLUcYvac0gAqFgiSnULCg00l1WEeiN2RiDlQShXtRVmR/benePZQUUI166WjwEGuevxZf8uMBvB/FibNqa/s1R+RA61gE+KtvBXxe6bcE2wFwqGi1zW57iBxKRQDQKkeyijQNavp1vKZHDF6Gqq/fW+/V4gry1DAHCgurpQQKB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PDB6jCMzudMyyYQBsnFZpxGW7hHm+pki0YHD0xw0UvJFIvmV3lXCEqell0G6pQCFRvXZ/EfqAmTEIMw02PRZtJhBDTG1UrWeODtuD+9Gv9ElC5MKcAGQpr4vr7XDbNyMBVOWefj+I9kJnMk9Q4Njeh9yHg0B1aW1i3b8ouGkComMKsfFU8/6cKU5NiiHHcb5wIDAQAB";

    //生成支付订单接口
    public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000150623708");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject("朱龙博测试支付宝");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("http://y6ac7c43.natappfree.cc/alipay/pay{" + payData.getCode() + "}");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code", 200);
            map.put("form", response.getBody());
        } else {
            map.put("code", 400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122628405");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
